﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using IFRS.DataAccess.DAL;
using System.Data;
using IFRS.Models;
using IFRS_Global;
using System.Globalization;

public partial class IFRS_WIN22_4A : PageBase
{
    #region "---------- Property -----------"

    private FeeAndCostType FeeAndCostType = new FeeAndCostType();
    private SPR_FEEANDCOSTTYPE SPR_FEEANDCOSTTYPE = new SPR_FEEANDCOSTTYPE();

    #endregion

    #region === Event ===

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            SetRequiredField();
            this.BindData();
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    protected void btnSAVE_NEW_Click(object sender, EventArgs e)
    {
        SaveData(true);
    }

    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData(false);
    }

    protected void btnCLEAR_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl(Request.Url.AbsoluteUri));
    }

    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN22_1S.aspx");
    }

    #endregion

    #region === Method ===

    private void InitPage()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN22_4A;
    }

    private void SetRequiredField()
    {
        btnSAVE.ValidationGroup = "Master";
        btnSAVE_NEW.ValidationGroup = "Master";
        reqFeeAndCostTypeCode.SetRequiredField(lblFeeAndCostTypeCode, txtFeeAndCostTypeCode, btnSAVE);
        reqFeeAndCostType.SetRequiredField(lblFeeAndCostType, txtFeeAndCostType, btnSAVE);
    }

    private void BindData()
    {
        if (Request.QueryString["id"] == null)
            return;

        var SPR_FEEANDCOSTTYPE = new SPR_FEEANDCOSTTYPE();
        DataTable dtFEEANDCOSTTYPE = SPR_FEEANDCOSTTYPE.SEARCH_BYID(Convert.ToInt32(Request.QueryString["id"]));
        List<FeeAndCostType> FeeAndCostType = new List<FeeAndCostType>();
        ConvertHelper.ConvertDataTableToObject(dtFEEANDCOSTTYPE, FeeAndCostType);
        if (dtFEEANDCOSTTYPE.Rows.Count > 0)
        {
            txtFeeAndCostTypeCode.Text = FeeAndCostType[0].FeeAndCostTypeCode;
            txtFeeAndCostType.Text = FeeAndCostType[0].FeeAndCostTypeName;

            if (FeeAndCostType[0].DataType == 1)
                radDataType1.Checked =true;
            else if(FeeAndCostType[0].DataType == 2)
                radDataType2.Checked = true;

            if (FeeAndCostType[0].IsIncludeAmortizeCost == true)
                radAmortizeCost1.Checked = true;
            else if (FeeAndCostType[0].IsIncludeAmortizeCost == false)
                radAmortizeCost2.Checked = true;
        }
    }

    private void SaveData(bool SaveNew)
    {
        FeeAndCostType.FeeAndCostTypeCode = txtFeeAndCostTypeCode.Text;
        FeeAndCostType.FeeAndCostTypeName = txtFeeAndCostType.Text;
   
        if (radDataType1.Checked == true)
            FeeAndCostType.DataType = 1;
        else if (radDataType2.Checked == true)
            FeeAndCostType.DataType = 2;

        if (radAmortizeCost1.Checked == true)
            FeeAndCostType.IsIncludeAmortizeCost = true;
        else if (radAmortizeCost2.Checked == true)
            FeeAndCostType.IsIncludeAmortizeCost = false;

        FeeAndCostType.SetStandardField();

        int iID = SPR_FEEANDCOSTTYPE.INSERT(FeeAndCostType);
        if (iID > 0)
        {
            if (SaveNew)
                this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(Request.Url.AbsoluteUri));
            else
                this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN22_2E.aspx?&id={0}", iID)));
        }
        else
        {
            if (iID == -1)
                Utility.AlertMessage(Page, Resources.ifrsResource.Duplicate);
            else
                Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
        }
    }

    #endregion
}